import * as https from 'https';
export default {
    /**
     * 生成一个32位随机数
     */
    genUUID: () => {
        const s: any[] = [];
        const hexDigits: string = '0123456789abcdef';
        for (let i = 0; i < 32; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = '4';
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
        s[8] = s[13] = s[18] = s[23];
        const uuid: string = s.join('');
        return uuid;
    },

    /**
     * 生成一个6位验证码
     */
    genCode: () => {
        const uuid = ('000000' + Math.floor(Math.random() * 999999)).slice(-6);
        return uuid;
    },

    /**
     * 生成一个20位随机数
     */
    genImgName: () => {
        const s: any[] = [];
        const hexDigits: string = '0123456789abcdef';
        for (let i = 0; i < 20; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = '4';
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
        s[8] = s[13] = s[18] = s[19];
        const uuid: string = s.join('');
        return uuid;
    },

    response(code: number, data?: any, msg?: string) {
        return {
            code,
            data,
            msg
        }
    },
    /**
     * 返回参数错误信息
     * @param msg 
     * @param data 
     */
    responseErrorParam(msg?: string, data?: any) {
        return this.response(4, data, msg);
    },
    /**
     * 返回成功信息
     * @param msg 
     * @param data 
     */
    responseSuccess(msg?: string, data?: any) {
        return this.response(1, data, msg);
    },
    /**
     * 返回操作失败信息
     * @param msg 
     */
    responseErrorMsg(msg?: string, data?: any) {
        return this.response(2, data, msg);
    },
    /**
     * 返回系统错误信息
     * @param msg 
     */
    responseSystemError(msg?: string) {
        return this.response(3, msg);
    },

    /**
     * 计算天数
     * @param timeStart 
     * @param timeEnd 
     */
    computeDate(timeStart, timeEnd) {
        const sArr = timeEnd.split("-");
        const eArr = timeStart.split("-");
        let sRDate: any = new Date(sArr[0], sArr[1], sArr[2]);
        let eRDate: any = new Date(eArr[0], eArr[1], eArr[2]);
        const days = (sRDate - eRDate) / (24 * 60 * 60 * 1000);
        console.log(days);
    },
    getDate(datestr) {
        var temp = datestr.split("-");
        var date = new Date(temp[0], temp[1] - 1, temp[2]);
        return date;
    },
    async getFullData(start, end) {
        let i = 0;
        let date_all: any = [];
        const startTime = this.getDate(start);
        const endTime = this.getDate(end);
        while ((endTime.getTime() - startTime.getTime()) >= 0) {
            const year = startTime.getFullYear();
            const month = (startTime.getMonth() + 1).toString().length == 1 ? "0" + (startTime.getMonth() + 1).toString() : (startTime.getMonth() + 1).toString();
            const day = startTime.getDate().toString().length == 1 ? "0" + startTime.getDate() : startTime.getDate();
            const item: any = {};
            item.date = year + "-" + month + "-" + day;
            item.total = 0;
            date_all.push(item);
            startTime.setDate(startTime.getDate() + 1);
            i = i + 1;
        }
        return date_all;
    },
    getMonthBetween(start, end) {
        let result: any = [];
        const s = start.split("-");
        const e = end.split("-");
        const min = new Date(s[0], s[1] - 1);
        const max = new Date(e[0], e[1] - 1);
        let curr = min;
        while (curr <= max) {
            let month = curr.getMonth();
            const item: any = {};
            item.date = curr.getFullYear() + "-" + ((month + 1) < 10 ? ("0" + (month + 1)) : month + 1);
            item.total = 0;
            result.push(item);
            curr.setMonth(month + 1);
        }
        return result;
    },
    wechatCode: (url) => {
        return new Promise((resolve) => {
            https.get(url, clientRes => {
                let reqData = '';
                clientRes.on('data', (data) => {
                    reqData += data;
                });
                clientRes.on('end', () => {
                    reqData = JSON.parse(reqData);
                    resolve(reqData);
                });
            })
        })
    },
    keyExg(param: any) {
        const exg = /{{(.*?)}}/gi;
        const arr = param.match(exg);
        const returnData: any = [];
        if (arr) {
            for (let i = 0; i < arr.length; i++) {
                const item = arr[i].split('');
                let newKey: string = '';
                for (let j = 0; j < item.length; j++) {
                    if (j === 0 || j === 1 || j === item.length - 1 || j === item.length - 2 || item[j].length === 0) {
                        continue;
                    }
                    if (item[j] === '.') {
                        break;
                    }
                    newKey = newKey + item[j];
                }
                returnData.push(newKey);
            }
        }
        return returnData;
    },
    labelExg(param: any) {
        const exg = /\n(.*?):/gi;
        const exg1 = /\n(.*?)：/gi;
        let arr = param.match(exg);
        if(!arr){
            arr = param.match(exg1);
        }
        const returnData: any = [];
        if (arr) {
            for (let i = 0; i < arr.length; i++) {
                const item = arr[i].split('');
                let newKey: string = '';
                for (let j = 0; j < item.length; j++) {
                    if (j === 0 || j === item.length - 1 || item[j].length === 0) {
                        continue;
                    }
                    newKey = newKey + item[j];
                }
                returnData.push(newKey);
            }
        }
        return returnData;
    },
    getMinutes(created_at: any, currentDate: any) {
        const stime = new Date(created_at).getTime();
        const etime = new Date(currentDate).getTime();
        const usedTime = etime - stime;  //两个时间戳相差的毫秒数	
        let minutes = usedTime / 60000;
        minutes = Number(minutes.toString().split('.')[0]);
        return minutes;
    },

}